import apiClient from './index';

// 查询考试列表参数类型
export interface ListExamsParams {
  class_id?: number;
  grade?: number;
  start_time?: string;
  end_time?: string;
}

// 考试列表项类型
export interface ExamItem {
  class_grade: number;
  class_name: string;
  class_id: number;
  deadline: string;
  exam_grade: number | null;
  exam_id: number;
  name: string;
  has_uploaded?: boolean;
  score?: number;
}

// 查询考试列表响应类型
export interface ListExamsResponse {
  code: number;
  content: ExamItem[];
  msg: string;
}

/**
 * 查询考试列表
 */
export const listExams = async (
  params: ListExamsParams
): Promise<ListExamsResponse> => {
  const response = await apiClient.post<ListExamsResponse>('/exam/list', params);
  return response.data;
};

// 查看考试学生排名参数（补充完整）
export interface GetExamStudentRankingParams {
  exam_ids: number[];
  class_id?: number;
}

// 考试学生排名项类型
export interface ExamStudentRankingItem {
  class_grade: number;
  class_name: string;
  student_id: number;
  student_name: string;
  student_number: string;
  total_score: number;
  rank: number;
}

// 考试学生排名响应类型
export interface GetExamStudentRankingResponse {
  code: number;
  content: ExamStudentRankingItem[];
  msg: string;
}

// 查看考试小组排名参数（补充完整）
export interface GetExamGroupRankingParams {
  exam_ids: number[];
  class_id?: number;
}

// 考试小组排名项类型
export interface ExamGroupRankingItem {
  class_id: number;
  class_name: string;
  group_id: number;
  group_name: string;
  total_score: number;
  average_score: number;
  rank: number;
}

// 考试小组排名响应类型
export interface GetExamGroupRankingResponse {
  code: number;
  content: ExamGroupRankingItem[];
  msg: string;
}

/**
 * 查看考试学生排名
 */
export const getExamStudentRanking = async (
  params: GetExamStudentRankingParams
): Promise<GetExamStudentRankingResponse> => {
  const response = await apiClient.post<GetExamStudentRankingResponse>('/exam/score-ranking', params);
  return response.data;
};

/**
 * 查看考试小组排名
 */
export const getExamGroupRanking = async (
  params: GetExamGroupRankingParams
): Promise<GetExamGroupRankingResponse> => {
  const response = await apiClient.post<GetExamGroupRankingResponse>('/exam/group-score-ranking', params);
  return response.data;
};

// 新增考试参数类型
export interface CreateExamParams {
  name: string;
  deadline: string;
  admin_password: string;
  class_id?: number;
  grade?: number;
}

// 编辑考试参数类型
export interface UpdateExamParams {
  exam_id: number;
  name: string;
  deadline: string;
  admin_password: string;
  class_id?: number;
  grade?: number;
}

// 新增/编辑考试响应类型
export interface ExamResponse {
  code: number;
  content: ExamItem;
  msg: string;
}

/**
 * 新增考试
 */
export const createExam = async (
  params: CreateExamParams
): Promise<ExamResponse> => {
  const response = await apiClient.post<ExamResponse>('/exam/create', params);
  return response.data;
};

/**
 * 编辑考试
 */
export const updateExam = async (
  params: UpdateExamParams
): Promise<ExamResponse> => {
  const response = await apiClient.post<ExamResponse>('/exam/update', params);
  return response.data;
};

// 修改或录入学生考试成绩参数
export interface UpdateExamScoreParams {
  exam_id: number;
  student_id: number;
  new_score: number;
  admin_password?: string; // 仅修改成绩时需要
}

export interface UpdateExamScoreResponse {
  code: number;
  content: {
    exam_id: number;
    exam_name: string;
    new_score: number;
    old_score?: number;
    student_id: number;
    student_name: string;
    updated_at: string;
    updated_by: string;
  };
  msg: string;
}

/**
 * 修改或录入学生考试成绩
 */
export const updateExamScore = async (
  params: UpdateExamScoreParams
): Promise<UpdateExamScoreResponse> => {
  const response = await apiClient.post<UpdateExamScoreResponse>('/exam/update-score', params);
  return response.data;
};

// 学生成绩排名请求参数
export interface StudentResultsParams {
  exam_ids: number[];
}

// 单个考试成绩详情
export interface ExamResultItem {
  class_rank: number | null;
  exam_id: number;
  exam_name: string;
  grade_rank: number | null;
  group_rank: number | null;
  score: number;
}

// 学生成绩排名整体返回结果
export interface StudentResultsResponse {
    code: number;
    msg: string;
    content: {
        class_id: number;
        class_name: string;
        exam_results: ExamResultItem[];
        group_id: number;
        group_name: string;
        student_id: number;
        student_name: string;
        total_class_rank: number | null;
        total_grade_rank: number | null;
        total_group_rank: number | null;
        total_score: number;
    };
}

/**
 * 获取学生成绩排名
 */
export const getStudentResults = async (
  params: StudentResultsParams
): Promise<StudentResultsResponse> => {
  const response = await apiClient.post<StudentResultsResponse>('/exam/student-results', params);
  return response.data;
};

// 学生录入成绩参数
export interface SubmitScoreParams {
    exam_id: number;
    score: number;
}

export interface SubmitScoreResponse {
    code: number;
    content: {
        exam_id: number;
        exam_name: string;
        score: number;
        student_id: number;
        student_name: string;
        submitted_at: string;
    },
    msg: string;
}

/**
 * 学生上传成绩
 */
export const submitScore = async (
  params: SubmitScoreParams
): Promise<SubmitScoreResponse> => {
  const response = await apiClient.post<SubmitScoreResponse>('/exam/submit-score', params);
  return response.data;
};
